/*
 * Copyright (c) 2024 iSOFT INFRASTRUCTURE SOFTWARE CO., LTD.
 * easyAda is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */

#ifndef __UAPI_CAPS_RAM_H__
#define __UAPI_CAPS_RAM_H__

//#include "dprintf.h" TODO remove
#include <plat/config.h>
#include <tools/macros.h>

#include "../bootinfo.h"
#include "capability.h"

enum cap_ram_type { CAP_RAM_KERNEL, CAP_RAM_NORMAL, CAP_RAM_DEVICE };

#define CAP_RAM_ATTR_BIT_D (0)

static inline unsigned int cap_ram_get_type(const capability_t *cap)
{
    return cap_get_short_info(cap);
}

static inline unsigned long cap_ram_get_desc(const capability_t *cap)
{
    return cap_get_long_info(cap) & (~1UL);
}

static inline int cap_ram_is_direct_map_only(const capability_t *cap)
{
    return cap_get_long_info(cap) & (1UL << CAP_RAM_ATTR_BIT_D);
}

static inline unsigned long cap_ram_get_size(const capability_t *cap)
{
    const region_t *desc = (region_t *)cap_ram_get_desc(cap);
    if (cap_ram_get_type(cap) == CAP_RAM_KERNEL) {
        return 0;
    } else {
        return desc->size;
    }
}

static inline unsigned long cap_ram_get_vbase(const capability_t *cap)
{
    const region_t *desc = (region_t *)cap_ram_get_desc(cap);

    if (cap_ram_get_type(cap) == CAP_RAM_KERNEL) {
        return 0;
    } else {
        return desc->vbase;
    }
}

static inline unsigned long cap_ram_get_pbase(const capability_t *cap)
{
    const region_t *desc = (region_t *)cap_ram_get_desc(cap);

    if (cap_ram_get_type(cap) == CAP_RAM_KERNEL) {
        return 0;
    } else {
        return desc->pbase;
    }
}

#endif
